package com.apes.fico.engine.timerTask;


import com.apes.fico.engine.model.*;
import com.apes.fico.engine.repository.TempVoucherRepository;
import com.apes.fico.finance.model.gl.GLVoucher;
import com.apes.fico.masterdata.gl.model.AccountingPeriodItem;
import com.apes.fico.masterdata.gl.respository.AccountingPeriodItemRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.util.MapUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BasisService extends DomainService {


    /*
    * TempVoucher 转成 ReduceVoucher
    * */
    public static ReduceVoucher toReduceVoucher(TempVoucher tempVoucher, String cycle, String cycleValue ) {
        ReduceVoucher reduceVoucher = new ReduceVoucher();
        reduceVoucher.setAccountingVoucherType(tempVoucher.getAccountingVoucherType());
        reduceVoucher.setCompany(tempVoucher.getCompany());
        reduceVoucher.changeDebitTotal(tempVoucher.getDebitTotal());
        reduceVoucher.changeCreditTotal(tempVoucher.getCreditTotal());
        reduceVoucher.setCycle(cycle);
        reduceVoucher.setCycleValue(cycleValue);
        reduceVoucher.setPostDate(tempVoucher.getPostDate());
        reduceVoucher.setRemark(tempVoucher.getRemark());
        reduceVoucher.setVoucherTemplate(tempVoucher.getVoucherTemplate());
        List<ReduceVoucherItem> reduceVoucherItems = new ArrayList<ReduceVoucherItem>();
        for(TempVoucherItem tempVoucherItem: tempVoucher.getVoucherItemList()) {
            ReduceVoucherItem reduceVoucherItem = toReduceVoucherItem(tempVoucherItem,reduceVoucher);
            reduceVoucherItems.add(reduceVoucherItem);
        }
        reduceVoucher.setVoucherItemList(reduceVoucherItems);
        return reduceVoucher;
    }



    /*
    * TempVoucherItem 转成 ReduceVoucherItem
    * */
    public static ReduceVoucherItem toReduceVoucherItem(TempVoucherItem tempVoucherItem,ReduceVoucher reduceVoucher) {
        ReduceVoucherItem reduceVoucherItem = new ReduceVoucherItem();
        String id = SpringManager.getBean(SequenceService.class).getId(ReduceVoucherItem.CONST_TABLE_NAME);
        reduceVoucherItem.setId(id);
        reduceVoucherItem.setVoucher(reduceVoucher);
        reduceVoucherItem.setAccountingOperatingChart(tempVoucherItem.getAccountingOperatingChart());
        reduceVoucherItem.setAux(tempVoucherItem.getCollectAux());
        reduceVoucherItem.setCredit(tempVoucherItem.getCredit());
        reduceVoucherItem.setDebit(tempVoucherItem.getDebit());
        reduceVoucherItem.setSummary(tempVoucherItem.getSummary());
        reduceVoucherItem.setVoucherTemplateEntry(tempVoucherItem.getVoucherTemplateEntry());
        List<TempVoucherAuxiliary> tempVoucherAuxiliaryList = tempVoucherItem.getVoucherAuxiliaryList();
        if(tempVoucherAuxiliaryList != null && !tempVoucherAuxiliaryList.isEmpty()) {
            List<ReduceVoucherAuxiliary> reduceVoucherAuxiliaryList = new ArrayList<ReduceVoucherAuxiliary>();
            for(TempVoucherAuxiliary tempVoucherAuxiliary: tempVoucherAuxiliaryList) {
                ReduceVoucherAuxiliary reduceVoucherAuxiliary = new ReduceVoucherAuxiliary();
                id = SpringManager.getBean(SequenceService.class).getId(ReduceVoucherAuxiliary.CONST_TABLE_NAME);
                reduceVoucherAuxiliary.setId(id);
                reduceVoucherAuxiliary.setAccountingOperatingChartAuxiliary(tempVoucherAuxiliary.getAccountingOperatingChartAuxiliary());
                reduceVoucherAuxiliary.setBusinessField(tempVoucherAuxiliary.getBusinessField());
                reduceVoucherAuxiliary.setCode(tempVoucherAuxiliary.getCode());
                reduceVoucherAuxiliary.setName(tempVoucherAuxiliary.getName());
                reduceVoucherAuxiliary.setVoucherItem(reduceVoucherItem);
                reduceVoucherAuxiliaryList.add(reduceVoucherAuxiliary);
            }
            reduceVoucherItem.setVoucherAuxiliaryList(reduceVoucherAuxiliaryList);
        }
        return reduceVoucherItem;
    }

    public String createGLVoucherAndApprove(ReduceVoucher reduceVoucher) {
        Map map = MapUtil.mapped(reduceVoucher);
        map.put("handworkFlag",false);
        AccountingPeriodItem accountingPeriodItem = SpringManager.getBean(AccountingPeriodItemRepository.class).findByPostDateAndCompany(reduceVoucher.getPostDate(), reduceVoucher.getCompany().getId());
        if (accountingPeriodItem == null) throw new RuntimeException("会计期间档案未定义此记帐日期！请检查");
        map.put("period", accountingPeriodItem.getPeriod_name());
        map.put("year", accountingPeriodItem.getAccountingPeriod().getYear());
        map.put("yearMonth", accountingPeriodItem.getYearMonth());
        map.put("month", accountingPeriodItem.getMonth());
        // 新增
        GLVoucher glVoucher = invoke("finance.GLVoucher.create",map);
        // 审核
        invoke("finance.GLVoucher.approve",MapUtil.mapped(glVoucher));
        return glVoucher.getId();
    }


    /*
    * 更新原始凭证已归约状态
    * */
    public static void updateTempVoucherState(TempVoucher tempVoucher) {
        tempVoucher.setState(true);
        SpringManager.getBean(TempVoucherRepository.class).saveAndFlush(tempVoucher);
    }







}
